package org.uugu.view;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.annotation.Resource;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.springframework.stereotype.Component;
import org.uugu.common.gui.DrawableUtils;
import org.uugu.voice.Listener;
import org.uugu.voice.UploadView;

/* The Java Version VoiceMan Project
 * All rights reserved.
 *
 * Licensed under the UUGU License, Version 2.0.1008.1034 (the "License");
 * you may not use this file except in compliance with the License(appId).
 * You may obtain an AppId of this application at
 *
 *      http://www.uugu.org
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Document we provide for details.
 */



/**VoiceMan Project
 * It's a project using MSC SDK, include tts, isr.
 * you can just press button to use it.
 * @author zhiguo.chen
 * 2012-06-14
 */
@SuppressWarnings("serial")
@Component
public class MainView extends JFrame implements ActionListener {		
	private JPanel mMainJpanel;
	private JPanel mContentPanel;
	public static JFrame mJframe;
	private JButton jbtKeyword;
	private JButton jbtRecognize;
	private JButton jbtSynthesize;
	
	@Resource
	private Listener listener;
	
	
	/**
	 * 界面初始化.
	 * 
	 */
	public MainView()
	{
//		Setting.saveLogFile(LOG_LEVEL.all, "./msc.log");
		//设置界面大小，背景图片
		ImageIcon background = new ImageIcon("res/index_bg.png");
		JLabel label = new JLabel(background);
		label.setBounds(0, 0, background.getIconWidth(), background
	                .getIconHeight());
		getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
		
		int frameWidth = background.getIconWidth();
		int frameHeight = background.getIconHeight();
		
		setSize(frameWidth, frameHeight);
		setResizable(false);
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
		
		ImageIcon imgRecognize = new ImageIcon("res/btn_recognize.png");
		jbtRecognize = this.createImageButton(imgRecognize);
		jbtRecognize.setBounds(60, 150, imgRecognize.getIconWidth(), imgRecognize.getIconHeight());	
		DrawableUtils.setMouseListener(jbtRecognize, "res/btn_recognize");
		
		ImageIcon imgKeyword = new ImageIcon("res/btn_keyword.png");
		jbtKeyword = this.createImageButton(imgKeyword);
		jbtKeyword.setBounds(240, 150, imgKeyword.getIconWidth(), imgKeyword.getIconHeight());
		DrawableUtils.setMouseListener(jbtKeyword, "res/btn_keyword");
		
		ImageIcon imgSynthesize = new ImageIcon("res/btn_synthesize.png");
		jbtSynthesize = this.createImageButton(imgSynthesize);
		jbtSynthesize.setBounds(420, 150, imgSynthesize.getIconWidth(), imgSynthesize.getIconHeight());
		DrawableUtils.setMouseListener(jbtSynthesize, "res/btn_synthesize");

		GridLayout gridlayout = new GridLayout(0, 3); 
		gridlayout.setHgap(10);   
		mMainJpanel = new JPanel(gridlayout);
		
		mMainJpanel.setOpaque(false);
		
		mMainJpanel.add(jbtRecognize);
		mMainJpanel.add(jbtKeyword);
		mMainJpanel.add(jbtSynthesize);
	
		jbtRecognize.addActionListener(this);
		jbtKeyword.addActionListener(this);
		jbtSynthesize.addActionListener(this);
		
		mContentPanel = new JPanel(new BorderLayout());
		mContentPanel.setOpaque(false);
		mContentPanel.add(mMainJpanel, BorderLayout.CENTER);
		
		setLocationRelativeTo(null); 
		setContentPane(mContentPanel);
		setTitle("VoiceMan V0.1");
		mJframe = this;
		setVisible(true);
	}
	
	public static JFrame getFrame()
	{
		return mJframe;
	}

	
	public JButton createImageButton(ImageIcon img)
	{
		JButton button = new JButton("");
		button.setIcon(img);
		button.setSize(img.getIconWidth(), img.getIconHeight());
		button.setBackground(null);
		
		button.setBorder(null);
		button.setContentAreaFilled(false);
		button.setFocusPainted(false);

		return button;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == jbtRecognize){
			mContentPanel.remove(mMainJpanel); 
			mContentPanel.add(listener); 
			mContentPanel.revalidate();
			mContentPanel.repaint(); 
		}else if(e.getSource() == jbtSynthesize){
			mContentPanel.remove(mMainJpanel); 
			mContentPanel.add(new SynthesizeView()); 
			mContentPanel.revalidate();
			mContentPanel.repaint(); 
		}else {
			mContentPanel.remove(mMainJpanel); 
			mContentPanel.add(new UploadView()); 
			mContentPanel.revalidate();
			mContentPanel.repaint(); 		
		}
	}
	
	public JPanel getMainJpanel()
	{
		return mMainJpanel;
	}
	
	public JPanel getContePanel()
	{
		return mContentPanel;
	}
}